package com.netease.xone.widget;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;

import com.netease.xone.R;
import com.netease.xone.adapter.AdapterPagerEmoticon;
import com.netease.xone.widget.ViewPagerChild.onEmoticonClickListener;
import com.netease.xone.widget.ViewPagerChild.onEmoticonTouchListener;



public class ViewPagerEmoticon extends LinearLayout {
    /** 表情View被Click的监听 */
    private onEmoticonClickListener mListener;
    /** 表情View被Touch的监听 */
    private onEmoticonTouchListener mOnEmoticonTouchListener;
    private ViewPager mViewPager;
    private BannerIndicator mBannerIndicator;
    private AdapterPagerEmoticon mAdapter;

    public ViewPagerEmoticon(Context context) {
        super(context);
        init();
    }

    public ViewPagerEmoticon(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        mAdapter = new AdapterPagerEmoticon();
        LinearLayout.LayoutParams param;
        setOrientation(VERTICAL);
        ViewPager viewPager = new ViewPager(getContext());
        viewPager.setAdapter(mAdapter);
        param = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT, getResources().getDimensionPixelSize(R.dimen.discuss_size_225));
        viewPager.setLayoutParams(param);
        addView(viewPager);
        mViewPager = viewPager;
        
        BannerIndicator indicator = (BannerIndicator)View.inflate(getContext(), R.layout.view_discuss_indicator, null);
        indicator.setBackgroundDrawable(null);
        indicator.setCount(3);
        indicator.setCurrentItem(0);
        param = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        param.gravity = Gravity.CENTER_HORIZONTAL;
        param.topMargin = getResources().getDimensionPixelSize(R.dimen.discuss_size_20);
        indicator.setLayoutParams(param);
        addView(indicator);
        mBannerIndicator = indicator;
        
        viewPager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageScrollStateChanged(int index) {
                
            }

            @Override
            public void onPageScrolled(int index, float arg1, int arg2) {
                
            }

            @Override
            public void onPageSelected(int index) {
                mBannerIndicator.setCurrentItem(index);
            }
            
        });
    }
    
    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (null != mAdapter) {
            mAdapter.setOnEmoticonClickListener(mListener);
            mAdapter.setOnEmoticonTouchListener(mOnEmoticonTouchListener);
        }
    }
    
    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(visibility);
        if (visibility == View.VISIBLE) {
            mViewPager.setCurrentItem(0);
            mBannerIndicator.setCurrentItem(0);
        }
    }
    
    /**
     * 为每个GridView设置表情Click监听
     * 
     * @param l
     */
    public void setOnEmoticonClickListener(onEmoticonClickListener l) {
        mListener = l;
    }

    /**
     * 为每个GridView设置表情Touch监听
     * 
     * @param l
     */
    public void setOnEmoticonTouchListener(onEmoticonTouchListener l) {
        mOnEmoticonTouchListener = l;
    }
}
